$(function(){
	 function createCode(){
        //首先默认code为空字符串
        var code = '';
        //设置随机字符
        var random =[0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
        //循环codeLength 设置的4就是循环4次
        for(var i = 0; i < 4; i++){
            //设置随机数范围,这设置为0 ~ 61
                var index = Math.floor(Math.random()*62);
                //字符串拼接 将每次随机的字符 进行拼接
                code += random[index]; 
        }
        return code;
    }
    $('#code').val(createCode());
    //设置此处的原因是每次进入界面展示一个随机的验证码，不设置则为空
    // 点击换一张展示一个新的验证码
    $('.logBbut').on("click",function(){ 
        $('#code').val(createCode());
    })
	// 短信验证码倒计时
	var time = 60;
	var var_new = $(".var_new")[0]
	var_new.onclick =timer;
	function timer(){
		this.value = (time--)+"秒后重新获取";
		this.disabled = true;
		this.onclick = "";
		m(this);
		function m(obj){
			setTimeout(function(){
				obj.value = (time--)+"秒后重新获取";
				if(time>(-1)){
					m(obj)
				}else{
					obj.value = "重新获取";
					obj.disabled = false;
					obj.onclick = timer;
					time=60;
				}
			},1000)
		}
	} 
	$.validator.addMethod("user",function(value,element){
		var reg = /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$|^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
		return this.optional(element) || value.match(reg);
	},"请输入正确的用户名")
	$.validator.addMethod("password",function(value,element){
         var reg = /^[a-zA-Z]\w{5,17}$/
         return this.optional(element) || value.match(reg);
	},"请输入正确格式密码")
	$("#form").validate({
		onkeyup:false,
		errorElement:"span",
		errorPlacement:function(error,element){
			$("#error_tip_"+element[0].name).append(error);
		},
		rules:{
			user:{
				required:true,
				user:true,
				remote:"../php/register_has.php"
			},
			checkbox:{
				required:true,
				equalTo:"#code"
			},
			password:{
				required:true,
				password:true
			},
			rpassword:{
				required:true,
				equalTo:"#password"
			},
			angree:{
				required:true
			}
		},
		messages:{
			user:{
				required:"请输入手机号码",
				user:"请输入正确的手机号码格式",
				remote:"该手机号已注册"
			},
			checkbox:{
				required:"请输入验证码",
				equalTo:"验证码错误"
			},
			password:{
				required:"请输入密码"
			},
			rpassword:{
				required:"请再次输入密码",
				rqualTo:"两次输入的密码不一致"
			},
			angree:{
				required:"请同意尚生活注册协议"
			}
		},
		submitHandler:function(){
			$.post("../php/register.php",{user:$("#user").val(),password:$("#password").val()},function(data){
					var data = JSON.parse(data);
					if(data.status == "success"){
						// location.href="../html/loginer.html"
						alert("nija")
					}
			})
			return false;
		}
	})
	
	$(".protocol").on("click",function(){
		$(".thickbox").fadeIn(500);
	})
	$("#closeBox").on("click",function(){
		$(".thickbox").fadeOut(500);
	})
	$(".btn-img").on("click",function(){
		$(".thickbox").fadeOut(500);
		$(".angree").attr("checked","true")
	})

	




})